-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13 - part1.py
More file actions
55 lines (44 loc) · 1.2 KB
/
day13 - part1.py
File metadata and controls
55 lines (44 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
file = open("input.txt")
lines = file.readlines()
max_x = 0
max_y = 0
points = []
folds = []
for line in lines:
line = line.replace("\n", "")
if line == "":
continue
if line.startswith("fold"):
axe, ind = line[11:].split("=")
folds.append((axe, int(ind)))
else:
x, y = list(map(int, line.split(",")))
points.append((x, y))
if x > max_x:
max_x = x
if y > max_y:
max_y = y
sheet = []
for y in range(max_y + 1):
line = [0] * (max_x + 1)
sheet.append(line)
for x, y in points:
sheet[y][x] = 1
def fold_y(sheet, ind):
for y, row in enumerate(sheet[ind + 1 :]):
for x, col in enumerate(row):
sheet[ind - y - 1][x] = int(1 in (col, sheet[ind - y - 1][x]))
return sheet[:ind]
def fold_x(sheet, ind):
new_sheet = []
for y, row in enumerate(sheet):
for x, col in enumerate(row[ind + 1 :]):
sheet[y][ind - x - 1] = int(1 in (col, sheet[y][ind - x - 1]))
new_sheet.append(sheet[y][:ind])
return new_sheet
axe, ind = folds[0]
if axe == "y":
sheet = fold_y(sheet, ind)
if axe == "x":
sheet = fold_x(sheet, ind)
print(sum(sum(x) for x in sheet))